# @Author ZhangGJ
# @Date 2021/12/01 18:47
import os
import sys

import cv2.cv2 as cv2
import numpy as np
import math


# 单模版匹配
def one_template():
    img = cv2.imread('../images/background.jpg')
    templ = cv2.imread('../images/template.png')
    height, width, c = templ.shape
    results = cv2.matchTemplate(img, templ, cv2.TM_SQDIFF_NORMED)
    min_value, max_value, min_loc, max_loc = cv2.minMaxLoc(results)
    result_point1 = min_loc
    result_point2 = (result_point1[0] + width, result_point1[1] + height)
    cv2.rectangle(img, result_point1, result_point2, (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def template2():
    image = []
    image.append(cv2.imread('../images/image_221.png'))
    image.append(cv2.imread('../images/image_222.png'))
    templ = cv2.imread('../images/templ.png')
    index = -1
    min = 1
    for i in range(0, len(image)):
        results = cv2.matchTemplate(image[i], templ, cv2.TM_SQDIFF_NORMED)
        if min > any(results[0]):
            index = i
    cv2.imshow('result', image[index])
    cv2.waitKey()
    cv2.destroyAllWindows()


# 查找重复图像
def template3():
    PIC_PATH = '/Users/zhanggj/Downloads/pycharm-projects/openvc-in-action/images/test/'
    width, height = 100, 100
    pic_file = os.listdir(PIC_PATH)
    same_pic_index = []
    imgs = []
    has_same = set()
    count = len(pic_file)
    if count == 0:
        print('没有图像')
        sys.exit(0)
    for file_name in pic_file:
        pic_name = PIC_PATH + file_name
        img = cv2.imread(pic_name)
        img = cv2.resize(img, (width, height))
        imgs.append(img)

    for i in range(0, count - 1):
        if i in has_same:
            continue
        templ = imgs[i]
        same = [i]
        for j in range(0 + i + 1, count):
            if j in has_same:
                continue
            pic = imgs[j]
            results = cv2.matchTemplate(pic, templ, cv2.TM_CCOEFF_NORMED)
            if results > 0.9:
                same.append(j)
                has_same.add(i)
                has_same.add(j)
        if len(same) > 1:
            same_pic_index.append(same)
    for same_list in same_pic_index:
        text = '相同的图片: '
        for same in same_list:
            text += str(pic_file[same]) + ', '
        print(text)


# 多目标匹配
def multi_template():
    img = cv2.imread('../images/background2.jpg')
    templ = cv2.imread('../images/template.png')
    height, width, c = templ.shape
    results = cv2.matchTemplate(img, templ, cv2.TM_CCOEFF_NORMED)
    for y in range(len(results)):
        for x in range(len(results[y])):
            if results[y][x] > 0.99:
                cv2.rectangle(img, (x, y), (x + width, y + height), (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def multi_template2():
    image = cv2.imread('../images/image.png')
    templ = cv2.imread('../images/templ1.png')
    height, width, c = templ.shape
    results = cv2.matchTemplate(image, templ, cv2.TM_CCOEFF_NORMED)
    station_num = 0
    for y in range(len(results)):
        for x in range(len(results[y])):
            if results[y][x] > 0.99:
                cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)
                station_num += 1
    cv2.putText(image, "The number of stations: " + str(station_num), (0, 30),
                cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
    cv2.imshow('result', image)
    cv2.waitKey()
    cv2.destroyAllWindows()


def multi_template3():
    image = cv2.imread('../images/image2.png')
    templ = cv2.imread('../images/templ2.png')
    height, width, c = templ.shape
    results = cv2.matchTemplate(image, templ, cv2.TM_CCOEFF_NORMED)
    point_x = []
    point_y = []
    for y in range(len(results)):
        for x in range(len(results[y])):
            if results[y][x] > 0.99:
                cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)
                point_x.extend([x])
                point_y.extend([y])
    start_x = 62
    start_y = 150
    place_square = np.array([point_x[0], point_y[0]])
    place_start = np.array([start_x, start_y])
    minus_ss = place_start - place_square
    start_square = math.hypot(minus_ss[0], minus_ss[1])
    place_highroad = np.array([point_x[1], point_y[1]])
    minus_hs = place_highroad - place_start
    start_highroad = math.hypot(minus_hs[0], minus_hs[1])
    if start_square < start_highroad:
        cv2.line(image, (start_x, start_y), (point_x[0], point_y[0]), (0, 255, 0), 2)
    else:
        cv2.line(image, (start_x, start_y), (point_x[1], point_y[1]), (0, 255, 0), 2)
    cv2.imshow('result', image)
    cv2.waitKey()
    cv2.destroyAllWindows()


def multi_template4():
    img = cv2.imread('../images/template/background2.jpg')
    templs = list()
    templs.append(cv2.imread('../images/template/template.png'))
    templs.append(cv2.imread('../images/template/template2.png'))
    templs.append(cv2.imread('../images/template/template3.png'))
    loc = list()
    for t in templs:
        loc += _matchTemplate(img, t)
    for i in loc:
        cv2.rectangle(img, (i[0], i[1]), (i[2], i[3]), (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def multi_template5():
    image = cv2.imread('../images/template1/image.png')
    templs = []
    templs.append(cv2.imread('../images/template1/car1.png'))
    templs.append(cv2.imread('../images/template1/car2.png'))
    templs.append(cv2.imread('../images/template1/car3.png'))
    templs.append(cv2.imread('../images/template1/car4.png'))
    for car in templs:
        results = cv2.matchTemplate(image, car, cv2.TM_CCOEFF_NORMED)
        for i in range(len(results)):
            for j in range(len(results[i])):
                if results[i][j] > 0.99:
                    if 0 < j <= 140:
                        print('车位编号: ', 1)
                    elif j <= 330:
                        print('车位编号: ', 2)
                    elif j <= 500:
                        print('车位编号: ', 3)
                    else:
                        print('车位编号: ', 4)
                    break


def _matchTemplate(img, templ):
    height, width, c = templ.shape
    results = cv2.matchTemplate(img, templ, cv2.TM_CCOEFF_NORMED)
    loc = list()
    for i in range(len(results)):
        for j in range(len(results[i])):
            if results[i][j] > 0.99:
                loc.append((j, i, j + width, i + height))
    return loc


if __name__ == '__main__':
    multi_template5()
